/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package HumanCentredSystemDesign;
import java.util.ArrayList;
import java.util.Date;
import java.util.Calendar;

/**
 *
 * @author Domonic and Chris
 */
public class LibraryCirculations {

    public final int max_loans = 6;
    static Connect connect1 = new Connect();
    static Copy myCopy = new Copy();
    static Loan myLoan = new Loan();
    static Book myBook = new Book();
    static Borrower myBorrower = new Borrower();
    static Periodical myPeriodical = new Periodical();
    static ArrayList<Borrower> myBorrowers;
    static ArrayList<Copy> myCopies;
    static ArrayList<Item> myItems;
    static ArrayList<Loan> myLoans;
    static ArrayList<Reservation> myReservations;

    public void execute() {
    }

    public void scan(String dewey) {
    }

    public Copy getCopyFromDewey (String dewey) {
       connect1.establishConnection();
       myCopies = connect1.getItemCopiesFromDatabase(dewey ,"");
       if(myCopies.isEmpty()){
           return null;
       }else {
       return myCopies.get(0);
       }
       
    }
    
    public boolean validDewey(String Dewey){
        myCopy = this.getCopyFromDewey(Dewey);
        if (myCopy.getDeweyIndex() != null) {
            return true;
        }
        else {
            return false;
       }
    }

    public Loan getLoanFromDewey (String dewey) {
        connect1.establishConnection();
        myLoans = connect1.getLoansFromDatabase("", dewey);
        if (myLoans.isEmpty() == true){
            return null;
        } else {
        return myLoans.get(0);
        }
    }

    public boolean isOnLoan(String dewey) {
        myLoan = getLoanFromDewey(dewey);
        if (myLoan == null) {
            return false;
        }
        else {
            return true;
            }
    }
    
    public Item getItemFromCopy(Copy c){

        try{
            String itemIdentifier = c.getItemIdentifier();
            connect1.establishConnection();
            if(itemIdentifier.length() == 10) {
                try{
                myItems = connect1.getBooksFromDatabase(itemIdentifier, "", "", "", "");
                connect1.closeConnection();
                return myItems.get(0);
                }
                catch(Exception err) { System.out.println("Can not get books from database: " + err.toString());
                return null; 
                }
            }
            else {
                try{
                    myItems = connect1.getPeriodicalsFromDatabase(itemIdentifier, "", "", "", "", "");
                    connect1.closeConnection();
                    return myItems.get(0);
                }
                catch(Exception err){
                    System.out.println("Can not get Periodicals from database: " + err.toString());
                    return null;
                }
            }
        }
        catch(Exception err)
        {
            System.out.println("Error getting Item : "+ err.toString());
            return null;
        }
    }
    
    public ArrayList<Item> getItemsFromDatabase(String idemidentifier,String title,String author,String publisher,String date,String volume,String number){
        ArrayList<Item> myList = new ArrayList<Item>();
        try
        {
            Connect myConn = new Connect();
            myConn.establishConnection();
            for (Item i : myConn.getBooksFromDatabase(idemidentifier, title, author, publisher, date) )
            {
                myList.add(i);
            }
            for (Item i : myConn.getPeriodicalsFromDatabase(idemidentifier, title, publisher, date, volume, number) )
            {
                myList.add(i);
            }
            
            myConn.closeConnection();
        }
        catch(Exception err){
            System.out.println("Error on getting items: "+ err.toString());
        }
        return myList;
    }

    public ArrayList<Borrower> getBorrower(String id, String forename, String surname, String email){
        try{
        connect1.establishConnection();
        myBorrowers= connect1.getBorrowersFromDatabase(id, forename, surname, email);
        return myBorrowers;
        }
        catch(Exception err){
            System.out.println("Error in getBorrower:" + err.toString());
            return new ArrayList<Borrower>();
        }
    }

    public ArrayList<String[]> overdue(Borrower b) {
        ArrayList<Loan> loanList = new ArrayList<Loan>();
        loanList = connect1.getBorrowersLoans(b);
        return calculateOverdue(loanList);
    }

    public void printOverdue(String[][] loans) {
        System.out.println("Overdue loans:");
        for (int i=0; i<loans.length; i++){
            System.out.println((i+1)+" - DeweyID: "+loans[i][0]+" MemberId: " + loans[i][1]);
        }
    }

    public ArrayList<String[]> calculateOverdue(ArrayList<Loan> loans) {
        
        String[] overdueLoan = new String[2];
        ArrayList allOverdues = new ArrayList();
        for(int i=0; i<loans.size(); i++) {
            //if statement for reserve
            Date due = incrementDate(loans.get(i).getIssueDate(), loans.get(i).getLoanPeriod());
            Date d = new Date();
            if(d.after(due)) {                              
                overdueLoan[0] = loans.get(i).getDeweyIndex();
                overdueLoan[1] = loans.get(i).getLibraryMemberID();
                allOverdues.add(overdueLoan);
            }
        }
        return allOverdues;
    }

    private Date incrementDate(Date d, int p) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(Calendar.DATE, p);
        d = cal.getTime();
        return d;
    }

    public ArrayList<Copy> getCopiesFromIdentifier(String id){
        if(id.length() == 10) {
            myBook.setItemIdentifier(id);
            myCopies = myBook.getItemCopies();

        }
        else {
            myPeriodical.setItemIdentifier(id);
            myCopies = myPeriodical.getItemCopies();
        }

        return myCopies;
    }

    public void printCopiesOnLoan(String id){
        myCopies = getCopiesFromIdentifier(id);
        for(int i=0; i<myCopies.size(); i++){
            System.out.println(myCopies.get(i) + "on Loan:" + myCopies.get(i).onLoan());
        }
    }

    public ArrayList<Copy> setLoanOfCopies(ArrayList<Copy> copies){
        ArrayList<Copy> copiesOnLoan = new ArrayList<Copy>();
        for(int i=0; i<copies.size(); i++){
            if(copies.get(i).onLoan() == true){
                copiesOnLoan.add(copies.get(i));
            }
        }
     return copiesOnLoan;
    }


}
